package ch.ffhs.ks.ui;

import java.awt.datatransfer.DataFlavor;

import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;

import ch.ffhs.ks.model.Folder;

public class DragDropHandler extends TransferHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4897244399345922358L;

	private MainWindow mainWindow;

	public DragDropHandler(MainWindow mainWindow)
	{
		this.mainWindow = mainWindow;
	}
	
	@Override
	public boolean canImport(JComponent arg0, DataFlavor[] arg1) {
		if(arg0 instanceof JTree)
		{
			return true;
		}

		return false;
	}

	@Override
	public boolean importData(TransferSupport support) {
		
		if(!support.isDrop())
			return false;
		
		JTree.DropLocation dropLocation = (JTree.DropLocation)support.getDropLocation();
		DefaultMutableTreeNode t = (DefaultMutableTreeNode)dropLocation.getPath().getLastPathComponent();
		Object userObject = t.getUserObject();
		
		if(userObject instanceof Folder)
		{
			this.mainWindow.moveSelectedMailsToFolder((Folder) userObject);
		}
		
		return true;
	}

}
